﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using todo.Model;

namespace gtasks.Model.Convert
{
    internal class TaskListToTodoList
    {
        public static TodoList Convert(TaskList taskList, Tasks tasks)
        {
            var todolist = new TodoList();

            todolist.Id = taskList.id;
//            todolist.Title = taskList.title;
            todolist.UpdateTime = taskList.updated;

            var todos = new List<Todo>();
            todolist.Todos = todos;

            var todoMap = new Dictionary<string, Todo>();
            foreach (var task in tasks.items)
            {
                var todo = new Todo();
                todoMap[task.id] = todo;

                todo.Id = task.id;
                todo.Title = task.title;
                //todo.Description = task.notes;
                UnpackNotes(todo, task.notes);

                var completed = task.status == "completed";
                todo.Checked = completed? 1: 0;

                //
                todo.CheckTime = task.completed;

                if (task.due != DateTime.MinValue)
                {
                    todo.Due = task.due.ToString("yyyy-MM-dd HH:mm");
                }

                if (string.IsNullOrEmpty(task.parent))
                {
                    todos.Add(todo);
                }
                else
                {
                    Todo parentTodo;
                    if(!todoMap.TryGetValue(task.parent, out parentTodo))
                    {
                        Debug.Print("Warning: task parent not exist! (" + task.id + "->" + task.parent + ")");
                        continue;
                    }
                    var parentTodos = parentTodo.Todos;
                    if(parentTodos == null)
                    {
                        parentTodos = parentTodo.Todos = new List<Todo>();
                    }
                    parentTodos.Add(todo);
                }
            }

            return todolist;
        }

        private static void UnpackNotes(Todo todo, string notes)
        {
            try
            {
                _UnpackNotes(todo, notes);
            }
            catch (Exception e)
            {
                Debug.Print("bad notes? " + notes);
                todo.Description = notes;
            }
        }

        private static void _UnpackNotes(Todo todo, string notes)
        {
            if (string.IsNullOrEmpty(notes))
            {
                return;
            }
            string attrStr = null;
            if (notes[0] == '{')
            {
                var pos2 = notes.IndexOf('}');
                if (pos2 != -1)
                {
                    attrStr = notes.Substring(1, pos2 - 1);
                    notes = notes.Substring(pos2 + 1);
                }
            }
            todo.Description = notes;
            if (attrStr == null)
            {
                return;
            }
            for (var n = 0; n < attrStr.Length; n++)
            {
                var c = attrStr[n];
                if (c == 'A') //active
                {
                    todo.Active = 1;
                }
                else if (c == 'P') //priority
                {
                    todo.Priority = int.Parse("" + attrStr[n + 1]);
                }
                else if (c == 'T') //tag
                {
                    var pos1 = n + 2;
                    var pos2 = attrStr.IndexOf(')', n);
                    n = pos2;
                    var sTags = attrStr.Substring(pos1, pos2 - pos1);
                    todo.Tags = new List<string>(sTags.Split(','));
                }
                else if (c == 'C') //color
                {
                    var pos1 = n + 2;
                    var pos2 = attrStr.IndexOf(')', n);
                    n = pos2;
                    var sColor = attrStr.Substring(pos1, pos2 - pos1);
                    todo.Color = sColor;
                }
                else if (c == 'U') // underline 
                {
                    todo.DisplayStyle |= DisplayStyle.Underline;
                }
                else if (c == 'B') // bold
                {
                    todo.DisplayStyle |= DisplayStyle.Bold;
                }
                else if (c == 'I') // italic 
                {
                    todo.DisplayStyle |= DisplayStyle.Italic;
                }
                else if (c == 'G') // progress
                {
                    var pos1 = n + 2;
                    var pos2 = attrStr.IndexOf(')', n);
                    n = pos2;
                    var sProgress = attrStr.Substring(pos1, pos2 - pos1);
                    todo.Progress = int.Parse(sProgress);
                }
            }
        }
    }
}